package netty.codec;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @Description TODO
 * @Author 李龙平
 * @Date 2020/10/12 10:42
 */
public class NettyClient {
    public static void main(String[] args) throws InterruptedException {
        //创建一个事件循环组
        EventLoopGroup eventExecutors = new NioEventLoopGroup();

        //创建客服端启动对象
        Bootstrap bootstrap = new Bootstrap();

        //设置参数
        try {
            bootstrap.group(eventExecutors)//设置线程组
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new NettyClientHandler()); //加入自己的处理器
                        }
                    });
            System.out.println("OK");
            //启动客服端 连接服务器
            ChannelFuture sync = bootstrap.connect("127.0.0.1", 6668).sync();
            //对关闭事件进行监听
            sync.channel().closeFuture().sync();

        } finally {
            eventExecutors.shutdownGracefully();
        }

    }
}
